<?php

class JobController extends AppController
{
    var $uses = array("Post", "Track", "Site", "Artist", "Track_artist");
    var $components = array('Autocomplete');
	var $helpers = array("Ajax");
	
    function multiple_track_artists($track_id, $save)
    {
		$this->delimeters = array(" ft ", " ft. ", " vs ", " vs. "," & ", "f/", " feat ", " feat. ", "featuring");
		
		$where = array();
		foreach ($this->delimeters as $item)
		{
			$where[] = "Track.name LIKE '%$item%'";
		}
		
		//$tracks = $this->Track->findAll(implode(" OR ", $where), null, null, 100);
		$track = $this->Track->find(array("Track.id" => $track_id));
		
//		foreach ($tracks as $track)
//		{
			$track_artists = array();

			foreach ($this->delimeters as $item)
			{
				if (stripos($track['Track']['artist'], $item))
				{
					$artists = explode($item, $track['Track']['artist']);
					
					foreach ($artists as $artist)
					{
						$new = ($this->try_artists($this->split_by_delimeter("track", $artist)));
						$new = array_merge($new, $this->try_artists(array($artist))); 

						foreach ($new as $i)
						{
							if (!in_array($i, $track_artists))
							{
								$track_artists[] = $i;
							}
						}
					}
					
				}
			}
			

			foreach ($this->delimeters as $item)
			{
				if (stripos($track['Track']['song'], $item))
				{
					$artists = explode($item, $track['Track']['song']);
					
					foreach ($artists as $artist)
					{
						$new = ($this->try_artists($this->split_by_delimeter("song", $artist)));
						$new = array_merge($new, $this->try_artists(array($artist))); 

						foreach ($new as $i)
						{
							if (!in_array($i, $track_artists))
							{
								$track_artists[$i] = $artist;
							}
						}
					}
					
				}
			}
			
			

			$this->Track_artist->query("DELETE FROM track_artists WHERE id = ".$track['Track']['id']);
			
			if ($save)
			{
				foreach ($track_artists as $i=>$artist)
				{
					if ($i==0) continue;
					$this->Track_artist->save(array("id" => null, "track_id" => $track['Track']['id'], "artist_id" => $artist["id"]));
				}
				
				$track["Track"]["artist_id"] = $track_artists[0]["id"];
				$track["Track"]["artist"] = $track_artists[0]["name"];
				$this->Track->save($track);
			}

//		}
		
		echo json_encode($track_artists);
		exit;
	}
	
	function view_track_artists($track_id)
	{
		$track = $this->Track->find(array("Track.id" => $track_id), null, null, 2);
		$primary_artist = $track['Artist'];
		$track_artists = $track['Track_artist'];
		
		$track_artists_full = array();
		foreach ($track_artists as $artist)
		{
			$track_artists_full[] = $this->Artist->find(array("Artist.id" => $artist["artist_id"]));
		}
			
		$this->set('track_artists', $track_artists_full);
		$this->set('track', $track);
		$this->layout = "ajax_html";
	}
	
	function set_primary_artist($track_id)
	{
		$artist_name = $this->data["Artist"]["name"];
		$artist = $this->Artist->find(array("Artist.name" => $artist_name));
		$track = $this->Track->find(array("Track.id" => $track_id));
		
		$track['Track']['artist_id'] = $artist['Artist']['id'];
		$track['Track']['artist'] = $artist['Artist']['name'];
		$this->Track->save($track);
		$this->requestAction("/job/view_track_artists/".$track_id);
		
		exit;
	}
	
	function delete_track_artist($track_id, $artist_id)
	{
		$artist = $this->Artist->find(array("Artist.id" => $artist_id));
		$this->Track_artist->deleteAll(array("Track_artist.track_id" => $track_id, "Track_artist.artist_id" => $artist['Artist']['id']));
		$this->requestAction("/job/view_track_artists/".$track_id);
		
		exit;		
	}

	function add_track_artist($track_id)
	{
		$artist_name = $this->data["Artist"]["name"];
		$artist = $this->Artist->find(array("Artist.name" => $artist_name));
		$track = $this->Track->find(array("Track.id" => $track_id));
		
		$track_artist['artist_id'] = $artist['Artist']['id'];
		$track_artist['track_id'] = $track_id;
		$track_artist['id'] = null;
		
		$this->Track_artist->save($track_artist);
		$this->requestAction("/job/view_track_artists/".$track_id);
		
		exit;
	}
	
	function natural_combi_words($phrase)
	{
		$arw = array();
		$words = explode(' ',trim($phrase));
		$nw = count ($words);
		for ($i=0; $i<$nw; $i++)
		{
		$k = "";
		$cnt = 0;
		for ($j=$i; $j<($nw); $j++)
		{
		$k .= $words[$j].' ';
		$arw[$cnt][] = trim($k);
		$cnt++;
		}
		}
		return $arw;
	}
	
	function try_artists($artists)
	{
		$found_artists = array();
		foreach ($artists as $artist)
		{
			$artist = str_replace("(", "", $artist);
			$artist = str_replace(")", "", $artist);
			
			$artist = mysql_real_escape_string(trim($artist));
			
			if ($found_artist = $this->Artist->find(array("Artist.name LIKE '$artist'"), array("Artist.name", "Artist.id")))
			{
				$found_artists[$found_artist['Artist']['id']] = $found_artist['Artist'];
			}
		}
		
		return $found_artists;
	}
	
	function split_by_delimeter($mode, $word)
	{
			foreach ($this->delimeters as $item)
			{
				if (stripos($word, $item))
				{
					return explode($item, $word);
				}
				
			}
		
	}
    
}
?>